package com.ys.controller.web.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bo.dto.NameValueSelectDto;
import com.ys.config.MenuId;
import com.ys.entity.sys.TsOrganizationInfo;
import com.ys.enume.sys.TsOrganizationInfoEnum;
import com.ys.service.sys.CommonApiService;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/sys/tsOrganizationInfo")
public class WebTsOrganizationInfoController {
	
	@Autowired
	private CommonApiService<TsOrganizationInfo> commonApiService;
	
	/**
	 * 新增
	 * @throws Exception 
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model,
			Long pid) throws Exception{
		
		TsOrganizationInfo entity = new TsOrganizationInfo().setId(pid).queryFirst();
		
		Map<String, Object> extraParam = new HashMap<String, Object>();
		extraParam.put("porgid", entity.getOrgid());
		extraParam.put("porgName", entity.getOrgName());
		
		return commonApiService.toAdd(model, new TsOrganizationInfo().setPid(pid), MenuId.TS_OrganizationInfo, extraParam, null);
	}
	
	/**
	 * 编辑
	 */
	@RequiresPermissions("tsOrganizationInfo:edit")
	@GetMapping("/toEdit")
	public String toEdit(Model model,
			Long id) throws Exception{
		
		
		Map<String, Object> extraParam = new TsOrganizationInfo().setId(id)
				.addLeftJoin("TS_UserInfo C on C.UserId = A.HeadUserId")
				
				.clearFieldsSource()
				.addField("C.UserName headUserName")
				.queryFirstMap();
		
		return commonApiService.toEdit(model, id, new TsOrganizationInfo(), MenuId.TS_OrganizationInfo, extraParam, null);
	}
	@Data
	@AllArgsConstructor
	private static class ToEditDto{
		private Long id; // GUID
		private String orgName; // 名称
		private List<NameValueSelectDto> orgKindArr;
	}
	
	/**
	 * 授权部门权限给用户
	 */
	@GetMapping("/toAuth")
	public String toAuth(Model model,
			Long id){
		
		TsOrganizationInfo tsOrganizationInfo = new TsOrganizationInfo().setId(id).queryFirst();
		
		String orgName = tsOrganizationInfo.getOrgName();
		
		List<NameValueSelectDto> orgKindArr = new ArrayList<NameValueSelectDto>();
		for (TsOrganizationInfoEnum.OrgKind e : TsOrganizationInfoEnum.OrgKind.values()) {
			orgKindArr.add(new NameValueSelectDto(e.name, e.value, false));
		}
		ToAuthDto dto = new ToAuthDto(id, orgName);
		
		model.addAttribute("dto", dto);
		return "sys/tsOrganizationInfo/toAuth";
	}
	@Data
	@AllArgsConstructor
	private static class ToAuthDto{
		private Long id;
		private String orgName;
	}
}
